package trader;
import trader.gui.*;
import trader.db.*;

public class BrokerControllerImpl
implements BrokerController
{
    private BrokerModel model;
    private BrokerView  view;
  
  /** Creates new BrokerControllerImpl */
    public BrokerControllerImpl(BrokerModel model, BrokerView view) {

        try {
            //** 1 Assign model to model
            this.model = model;

            //** 2 Assign view to view
            this.view = view;

            //** 3 Register this object as a user gesture listener with
            //**   the view object
            //**   Hint - invoke addUserGestureListener
            view.addUserGestureListener(this);

        }//try

        catch(Exception e) {
            reportException(e);
        }//catch

    }//BrokerControllerImpl Constructor


    private void reportException(Object o) {
        // The responsibility of this method is to report exceptions
        // It calls the view's showDisplay method
        try {
            view.showDisplay(o);
        }//try

        catch(Exception e) {
            System.out.println("BrokerControllerImpl reportException " + e);
        }//catch

    }//reportException()
    
  
    //user gesture call back methods
    /* ---------------------------------------------------------------
    * get customer user gesture handle method called by the broker
    * view in response to the get customer button click on the GUI or
    * equivalent user interface.
    * action - set customer display on the gui through the
    * showDisplay method of the broker view
    */
    public void handleGetCustomerGesture(String id) {

        System.out.println("handleGetCustomerGesture " + id);
        Customer cust = null;
        
        try {

            //** 1 Set cust to the object returned as a result of
            //**   invoking the getCustomer method on model
            cust = model.getCustomer(id);

            //** 2 Invoke showDisplay method of view with cust
            //**   as parameter
            view.showDisplay(cust);

        }//try

        catch(Exception e) {
            e.printStackTrace();
            reportException(e);
        }//catch

    }//handleGetCustomerGesture()

  
    /* ---------------------------------------------------------------
    * add new customer user gesture handle method called by the
    * broker view in response to the add customer button click on the
    * GUI or equivalent user interface.
    * action - add the (new) customer customer to the model
    */
    public void handleAddCustomerGesture(Customer c) {

        System.out.println("handleAddCustomerGesture " + c);

        try {
            //** 1 Invoke addCustomer method of model with c
            //**   as parameter
            model.addCustomer(c);

        }//try

        catch(Exception e) {
            reportException(e);
        }//catch

    }//handelAddCustomerGesture()

  
    /* ---------------------------------------------------------------
    * delete customer user gesture  handle method called by
    * the broker view in response to the delete customer
    * button click on the GUI or equivalent user interface
    * action  - delete the customer from the model
    */
    public void handleDeleteCustomerGesture(Customer c){

        System.out.println("handleDeleteCustomerGesture " + c);

        try {
            //** 1 Invoke deleteCustomer method of model with c
            //**   as parameter
            model.deleteCustomer(c);

        }//try

        catch(Exception e) {
            reportException(e);
        }//catch

    }//handleDeleteCustomerGesture()

  
    /* ---------------------------------------------------------------
    * update customer user gesture callback method called by
    * the broker view in response to the update customer
    * button click on the GUI or equivalent user interface
    * action  - update the customer in the model
    */
    public void handleUpdateCustomerGesture(Customer c){

        System.out.println("handleUpdateCustomerGesture " + c);

        try {
            //** 1 Invoke updateCustomer method of model with c
            //**   as parameter
            model.updateCustomer(c);

        }//try

        catch(Exception e) {
            reportException(e);
        }//catch

    }//handleUpdateCustomerGesture()

  
    /* ---------------------------------------------------------------
    * get all customers user gesture callback method called
    * the broker view in response to the get all customers
    * button click on the GUI or equivalent user interface
    * action - set all customers display on the gui through the
    * showDisplay method of the broker view
    */
    public void handleGetAllCustomersGesture(){

        System.out.println("handleGetAllCustomerGesture ");

        Customer custs[];

        try {
            //** 1 Invoke getAllCustomers method of model
            //**   Assign the return value from this method to custs
            custs = model.getAllCustomers();

            //** 2 Invoke showDisplay method of view with custs
            //** as parameter
            view.showDisplay(custs);

        }//try

        catch(Exception e) {
            reportException(e);
        }//catch

    }//handleGetAllCustomersGesture()

}//BrokerControllerImpl Class